<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
                "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
  <title>Description of gmmactiv</title>
  <meta name="keywords" content="gmmactiv">
  <meta name="description" content="GMMACTIV Computes the activations of a Gaussian mixture model.">
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <meta name="generator" content="m2html &copy; 2003 Guillaume Flandin">
  <meta name="robots" content="index, follow">
  <link type="text/css" rel="stylesheet" href="../../m2html.css">
</head>
<body>
<a name="_top"></a>
<div><a href="../../menu.html">Home</a> &gt;  <a href="#">ReBEL-0.2.7</a> &gt; <a href="#">netlab</a> &gt; gmmactiv.m</div>

<!--<table width="100%"><tr><td align="left"><a href="../../menu.html"><img alt="<" border="0" src="../../left.png">&nbsp;Master index</a></td>
<td align="right"><a href="menu.html">Index for .\ReBEL-0.2.7\netlab&nbsp;<img alt=">" border="0" src="../../right.png"></a></td></tr></table>-->

<h1>gmmactiv
</h1>

<h2><a name="_name"></a>PURPOSE <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="box"><strong>GMMACTIV Computes the activations of a Gaussian mixture model.</strong></div>

<h2><a name="_synopsis"></a>SYNOPSIS <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="box"><strong>function a = gmmactiv(mix, x) </strong></div>

<h2><a name="_description"></a>DESCRIPTION <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="fragment"><pre class="comment">GMMACTIV Computes the activations of a Gaussian mixture model.

    Description
    This function computes the activations A (i.e. the  probability
    P(X|J) of the data conditioned on each component density)  for a
    Gaussian mixture model.  For the PPCA model, each activation is the
    conditional probability of X given that it is generated by the
    component subspace. The data structure MIX defines the mixture model,
    while the matrix X contains the data vectors.  Each row of X
    represents a single vector.

    See also
    <a href="gmm.html" class="code" title="function mix = gmm(dim, ncentres, covar_type, ppca_dim)">GMM</a>, <a href="gmmpost.html" class="code" title="function [post, a] = gmmpost(mix, x)">GMMPOST</a>, <a href="gmmprob.html" class="code" title="function prob = gmmprob(mix, x)">GMMPROB</a></pre></div>

<!-- crossreference -->
<h2><a name="_cross"></a>CROSS-REFERENCE INFORMATION <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
This function calls:
<ul style="list-style-image:url(../../matlabicon.gif)">
<li><a href="consist.html" class="code" title="function errstring = consist(model, type, inputs, outputs)">consist</a>	CONSIST Check that arguments are consistent.</li><li><a href="dist2.html" class="code" title="function n2 = dist2(x, c)">dist2</a>	DIST2	Calculates squared distance between two sets of points.</li></ul>
This function is called by:
<ul style="list-style-image:url(../../matlabicon.gif)">
<li><a href="demev2.html" class="code" title="">demev2</a>	DEMEV2	Demonstrate Bayesian classification for the MLP.</li><li><a href="demgpot.html" class="code" title="function g = demgpot(x, mix)">demgpot</a>	DEMGPOT Computes the gradient of the negative log likelihood for a mixture model.</li><li><a href="demmlp2.html" class="code" title="">demmlp2</a>	DEMMLP2 Demonstrate simple classification using a multi-layer perceptron</li><li><a href="gmmpost.html" class="code" title="function [post, a] = gmmpost(mix, x)">gmmpost</a>	GMMPOST Computes the class posterior probabilities of a Gaussian mixture model.</li><li><a href="gmmprob.html" class="code" title="function prob = gmmprob(mix, x)">gmmprob</a>	GMMPROB Computes the data probability for a Gaussian mixture model.</li></ul>
<!-- crossreference -->


<h2><a name="_source"></a>SOURCE CODE <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="fragment"><pre>0001 <a name="_sub0" href="#_subfunctions" class="code">function a = gmmactiv(mix, x)</a>
0002 <span class="comment">%GMMACTIV Computes the activations of a Gaussian mixture model.</span>
0003 <span class="comment">%</span>
0004 <span class="comment">%    Description</span>
0005 <span class="comment">%    This function computes the activations A (i.e. the  probability</span>
0006 <span class="comment">%    P(X|J) of the data conditioned on each component density)  for a</span>
0007 <span class="comment">%    Gaussian mixture model.  For the PPCA model, each activation is the</span>
0008 <span class="comment">%    conditional probability of X given that it is generated by the</span>
0009 <span class="comment">%    component subspace. The data structure MIX defines the mixture model,</span>
0010 <span class="comment">%    while the matrix X contains the data vectors.  Each row of X</span>
0011 <span class="comment">%    represents a single vector.</span>
0012 <span class="comment">%</span>
0013 <span class="comment">%    See also</span>
0014 <span class="comment">%    GMM, GMMPOST, GMMPROB</span>
0015 <span class="comment">%</span>
0016 
0017 <span class="comment">%    Copyright (c) Ian T Nabney (1996-2001)</span>
0018 
0019 <span class="comment">% Check that inputs are consistent</span>
0020 errstring = <a href="consist.html" class="code" title="function errstring = consist(model, type, inputs, outputs)">consist</a>(mix, <span class="string">'gmm'</span>, x);
0021 <span class="keyword">if</span> ~isempty(errstring)
0022   error(errstring);
0023 <span class="keyword">end</span>
0024 
0025 ndata = size(x, 1);
0026 a = zeros(ndata, mix.ncentres);  <span class="comment">% Preallocate matrix</span>
0027 
0028 <span class="keyword">switch</span> mix.covar_type
0029   
0030 <span class="keyword">case</span> <span class="string">'spherical'</span>
0031   <span class="comment">% Calculate squared norm matrix, of dimension (ndata, ncentres)</span>
0032   n2 = <a href="dist2.html" class="code" title="function n2 = dist2(x, c)">dist2</a>(x, mix.centres);
0033   
0034   <span class="comment">% Calculate width factors</span>
0035   wi2 = ones(ndata, 1) * (2 .* mix.covars);
0036   normal = (pi .* wi2) .^ (mix.nin/2);
0037   
0038   <span class="comment">% Now compute the activations</span>
0039   a = exp(-(n2./wi2))./ normal;
0040   
0041 <span class="keyword">case</span> <span class="string">'diag'</span>
0042   normal = (2*pi)^(mix.nin/2);
0043   s = prod(sqrt(mix.covars), 2);
0044   <span class="keyword">for</span> j = 1:mix.ncentres
0045     diffs = x - (ones(ndata, 1) * mix.centres(j, :));
0046     a(:, j) = exp(-0.5*sum((diffs.*diffs)./(ones(ndata, 1) * <span class="keyword">...</span>
0047       mix.covars(j, :)), 2)) ./ (normal*s(j));
0048   <span class="keyword">end</span>
0049   
0050 <span class="keyword">case</span> <span class="string">'full'</span>
0051   normal = (2*pi)^(mix.nin/2);
0052   <span class="keyword">for</span> j = 1:mix.ncentres
0053     diffs = x - (ones(ndata, 1) * mix.centres(j, :));
0054     <span class="comment">% Use Cholesky decomposition of covariance matrix to speed computation</span>
0055     c = chol(mix.covars(:, :, j));
0056     temp = diffs/c;
0057     a(:, j) = exp(-0.5*sum(temp.*temp, 2))./(normal*prod(diag(c)));
0058   <span class="keyword">end</span>
0059 <span class="keyword">case</span> <span class="string">'ppca'</span>
0060   log_normal = mix.nin*log(2*pi);
0061   d2 = zeros(ndata, mix.ncentres);
0062   logZ = zeros(1, mix.ncentres);
0063   <span class="keyword">for</span> i = 1:mix.ncentres
0064     k = 1 - mix.covars(i)./mix.lambda(i, :);
0065     logZ(i) = log_normal + mix.nin*log(mix.covars(i)) - <span class="keyword">...</span>
0066       sum(log(1 - k));
0067     diffs = x - ones(ndata, 1)*mix.centres(i, :);
0068     proj = diffs*mix.U(:, :, i);
0069     d2(:,i) = (sum(diffs.*diffs, 2) - <span class="keyword">...</span>
0070       sum((proj.*(ones(ndata, 1)*k)).*proj, 2)) / <span class="keyword">...</span>
0071       mix.covars(i);
0072   <span class="keyword">end</span>
0073   a = exp(-0.5*(d2 + ones(ndata, 1)*logZ));
0074 <span class="keyword">otherwise</span>
0075   error([<span class="string">'Unknown covariance type '</span>, mix.covar_type]);
0076 <span class="keyword">end</span>
0077</pre></div>
<hr><address>Generated on Tue 26-Sep-2006 10:36:21 by <strong><a href="http://www.artefact.tk/software/matlab/m2html/">m2html</a></strong> &copy; 2003</address>
</body>
</html>